package com.atguigu.cloud.controller;

import com.atguigu.cloud.entities.Pay;
import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.response.ResponseCodeEnum;
import com.atguigu.cloud.response.ResultData;
import com.atguigu.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Tag(name = "支付微服务模块", description = "支付交易CRUD")
@RequestMapping("/pay")
@RestController
@Slf4j
public class PayController {

    @Resource
    private PayService payService;

    /**
     * 新增支付信息
     *
     * @param payDTO 支付信息数据传输对象
     * @return 新增消息
     */
    @PostMapping("/add")
    @Operation(summary = "新增支付信息", description = "新增操作")
    @Parameter(name = "payDTO", description = "支付信息数据传输对象")
    public ResultData addPay(@RequestBody PayDTO payDTO) {
        log.info("新增支付信息：" + payDTO.toString());
        // 拷贝数据
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO, pay);
        // 新增操作
        int i = payService.add(pay);
        return i > 0 ? ResultData.success(i, ResponseCodeEnum.RC201) : ResultData.fail("新增失败");
    }

    /**
     * 删除支付信息
     *
     * @param id 支付id
     * @return 删除消息
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除支付信息", description = "删除操作")
    @Parameter(name = "id", description = "支付id", required = true, example = "1")
    public ResultData deletePay(@PathVariable("id") Integer id) {
        log.info("删除支付信息：" + id);
        int i = payService.delete(id);
        return i > 0 ? ResultData.success(i, ResponseCodeEnum.RC204) : ResultData.fail("删除失败");
    }

    /**
     * 更新支付信息
     *
     * @param pay 支付信息
     * @return 更新消息
     */
    @PutMapping("/update")
    @Operation(summary = "更新支付信息", description = "更新操作")
    @Parameter(name = "pay", description = "支付信息")
    public ResultData updatePay(@RequestBody Pay pay) {
        log.info("更新支付信息：" + pay.toString());
        pay.setUpdateTime(LocalDateTime.now());
        // 更新操作
        int i = payService.update(pay);
        return i > 0 ? ResultData.success(i, "更新成功") : ResultData.fail("更新失败");
    }

    /**
     * 根据id查询支付信息
     *
     * @param id 支付id
     * @return 支付信息
     */
    @GetMapping("/getById/{id}")
    @Operation(summary = "根据id查询支付信息", description = "根据id查询操作")
    @Parameter(name = "id", description = "支付id", required = true, example = "1")
    public ResultData<Pay> getPayById(@PathVariable("id") Integer id) {
        log.info("根据id查询支付信息：" + id);
        return ResultData.success(payService.getById(id));
    }

    /**
     * 查询所有支付信息
     *
     * @return 支付信息集合
     */
    @GetMapping("/getAll")
    @Operation(summary = "查询所有支付信息", description = "查询操作")
    public ResultData<List<Pay>> getAllPay() {
        List<Pay> payList = payService.getAll();
        log.info("查询所有支付信息：" + payList.toString());
        return ResultData.success(payList);
    }
}
